<?php
	include("header.php");
	unset($_SESSION['message']);
	if(!isset($_GET['search']))
		$_GET['search'] = '';
?>
	<div id="content">
		<div id="title"><b>Производители</b></div>

		<form name="fsearch" method="GET">
			<fieldset class="fieldsetFilter">
				<legend>Филтър</legend>
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
					<tr>
						<td height="30"><span class="labelFilter">Търсене:</span>&nbsp;</td>
						<td width="665"><input type="text" name="search" value="<?php echo trim($_GET['search']); ?>" class="input" style="width: 665px;" /></td>
						<td align="right"><input type="submit" class="btn" value="Търсене" /></td>
					</tr>
				</table>
			</fieldset>
		</form>

		<div id="getContent">
			<div id="topAddImg">
				<a href="manufacturers_edit.php?manufacturer_id=0">
					<img src="images/add_car.png" width="24" height="24" alt="" title="Добави" />
					<span>Добави</span>
				</a>
			</div>
			<?php
				// Странициране
				$per_page = 15;
					
				if(isset($_GET['page'])){
					$stranica	= $_GET['page'];
					$start		= ($stranica-1)*$per_page;
				}
				else
					$start = 0;
				
				if(isset($_GET['page']))
					$page = $_GET['page'] + 1;
				else
					$page = 1;

				// Производители
				if(isset($_GET['search']) && (!empty($_GET['search']))){
					$query	= sprintf("SELECT `manufacturer_id`, `name`, `created`
							   FROM `manufacturers`
							   WHERE (UPPER(`name`) LIKE '%%%s%%' )
							   AND `deleted` = 0
							   ORDER BY `name` ASC
							   LIMIT %d, %d", trim($_GET['search']), $start, $per_page);
				}
				else{
					$query	= sprintf("SELECT `manufacturer_id`, `name`, `created`
							   FROM `manufacturers`
							   WHERE `deleted` = 0
							   ORDER BY `name` ASC
							   LIMIT %d, %d", $start, $per_page);
				}
				
				$results	= mysql_query($query) or die(mysql_error());
				$num_rows	= mysql_num_rows($results);

				if($num_rows)
				{
			?>
					<table id="tbl" border="0" width="100%" cellpadding="0" cellspacing="0">
						<tr align="center">
							<th width="30">№</th>
							<th width="350">Производител</th>
							<th width="100">Дата</th>
							<th width="40">Действие</th>
						</tr>
			<?php
						$i = 0;		
						while($fetch = mysql_fetch_assoc($results))
						{
							$i++;
							$bgcolor = (($i % 2 == 0) ? '#dbdbdb' : '#ffffff');

							if(isset($_GET['page'])){
								if($_GET['page']==1)
									$left_rec = $i;
								else
									$left_rec = ((($page - 1)*$per_page)+$i)-$per_page;
							}
							else
								$left_rec = (($page - 1) * 2)+$i;
			?>
							<tr>
								<td bgcolor="<?php echo $bgcolor; ?>" height="40"><?php echo $left_rec; ?></td>
								<td bgcolor="<?php echo $bgcolor; ?>">
									<a href="manufacturers_edit.php?manufacturer_id=<?php echo $fetch['manufacturer_id']; ?>" >
										<?php echo stripslashes($fetch['name']); ?>
									</a>
								</td>
								<td bgcolor="<?php echo $bgcolor; ?>">
									<?php echo date('d-m-Y H:i:s', $fetch['created']); ?>
								</td>
								<td bgcolor="<?php echo $bgcolor; ?>" onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='normal'">
									<a href="manufacturers_edit.php?manufacturer_id=<?php echo $fetch['manufacturer_id']; ?>" >
										<img src="images/edit.png" width="24" height="24" alt="" title="Редактирай" />
									</a>
									<img src="images/delete.png" width="24" height="24" alt="" title="Изтрии" onClick="deleteRowTable(<?php echo $fetch['manufacturer_id']; ?>, <?php echo $i; ?>, 'manufacturers');" />
								</td>
							</tr>
		<?php
						}
		?>
					</table>

					<div id="pager">
						<?php
							// Брой производители
							if( (isset($_GET['search'])) && (!empty($_GET['search'])) )
							{
								$search = "&search=".trim($_GET['search']);

								$qty	= sprintf("SELECT COUNT(*) as cnt
										   FROM `manufacturers`
										   WHERE (UPPER(`name`) LIKE '%%%s%%' )
										   AND `deleted` = 0
										   ORDER BY `name` ASC", trim($_GET['search']));
							}
							else{
								$search = '';
								$qty	= sprintf("SELECT COUNT(*) as cnt
										   FROM `manufacturers`
										   WHERE (UPPER(`name`) LIKE '%%%s%%' )
										   AND `deleted` = 0
										   ORDER BY `name` ASC", trim($_GET['search']));
							}

							$rez	= mysql_query($qty);

							if(mysql_num_rows($rez))
							{
								$pagerData = mysql_fetch_assoc($rez);

								$total_pages = ceil(($pagerData['cnt']/$per_page));

								if(isset($_GET['page']))
									$current_page	= $_GET['page'];
								else
									$current_page = 1;
								$grace	= 5;			// 5 pages on the left and 5 pages on the right of current page
								$range	= $grace*2;

								$start	= ($current_page - $grace) > 0 ? ($current_page - $grace) : 1;
								$end		= $start + $range;

								if($end > $total_pages){	//make sure $end doesn't go beyond total pages
									$end	 = $total_pages;
									$start = ($end - $range) > 0 ? ($end - $range) : 1;	//if there is a change in $end, adjust $start again
								}

								if($start>1){
									echo "<a href='?page=1'>1</a> ...";
								}

								for($i=$start;$i<=$end;$i++)
								{
									if($i == $current_page){
										echo "<span>$i</span>&nbsp;&nbsp;";		// Current page is not clickable and different from other pages
									}
									else {
										if(($i==1) && !isset($_GET['page']))
											echo "<span>$i</span>&nbsp;&nbsp;";
										else
											echo "<a href='?page=$i$search'>$i</a>&nbsp;&nbsp;";
									}
								}

								if($end < $total_pages){
									echo "... <a href='?page=$total_pages$search'>$total_pages</a>";	// If $end is away from total pages, add a link of the last page
								}
							}
						?>
						</div>
		<?php
					}
					else{
		?>
						<table id="tbl" border="0" width="100%" cellpadding="0" cellspacing="0">
							<tr align="center">
								<th width="30">№</th>
								<th width="350">Производител</th>
								<th width="100">Дата</th>
								<th width="40">Действие</th>
							</tr>
							<tr>
								<td style="padding:10px; text-align: center; color: #ff0000;" colspan="5">Няма намерени резултати!</td>
							</tr>
						</table>		
		<?php
					}
		?>
		</div>

		<?php require_once("footer.php"); ?>